/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sample.jws;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

/**
 *
 * @author 6789
 */
@WebService()
public class LoginService {

    /**
     * Web service operation
     */
    @WebMethod(operationName = "checkLogin")
    public boolean checkLogin(@WebParam(name = "username") String username, @WebParam(name = "password") String password) {
        try {
            //TODO write your implementation code here:
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=Account", "sa", "1");
            PreparedStatement stm = null;
            ResultSet rs = null;
            stm = con.prepareStatement("SELECT * FROM Accounts WHERE Username=? AND Password=?");
            stm.setString(1, username);
            stm.setString(2, password);
            rs = stm.executeQuery();
            if (rs.next()) {
                return true;
            }//khung la chet

        } catch (SQLException ex) {
            Logger.getLogger(LoginService.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(LoginService.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "insert")
    public boolean insert(@WebParam(name = "user") String user, @WebParam(name = "pass") String pass) {
        try {
            //TODO write your implementation code here:
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=Account", "sa", "1");
            PreparedStatement stm = null;
            stm = con.prepareStatement("INSERT INTO Accounts VALUES(?,?)");
            stm.setString(1, user);
            stm.setString(2, pass);
            int rs = stm.executeUpdate();
            if (rs != 0) {
                return true;
            }

        } catch (SQLException ex) {
            Logger.getLogger(LoginService.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(LoginService.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "update")
    public boolean update(@WebParam(name = "user") String user, @WebParam(name = "pass") String pass) {
        try {
            //TODO write your implementation code here:
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=Account", "sa", "1");
            PreparedStatement stm = null;
            stm = con.prepareStatement("UPDATE Accounts SET Password=? WHERE Username=?");
            stm.setString(2, user);
            stm.setString(1, pass);
            int rs = stm.executeUpdate();
            if (rs != 0) {
                return true;
            }

        } catch (SQLException ex) {
            Logger.getLogger(LoginService.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(LoginService.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "delete")
    public boolean delete(@WebParam(name = "username") String username) {
        try {
            //TODO write your implementation code here:
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=Account", "sa", "1");
            PreparedStatement stm = null;
            stm = con.prepareStatement("DELETE Accounts WHERE Username=?");
            stm.setString(1, username);
            int rs = stm.executeUpdate();
            if (rs != 0) {
                return true;
            }

        } catch (SQLException ex) {
            Logger.getLogger(LoginService.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(LoginService.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "search")
    public String search(@WebParam(name = "username") String username) {
        try {
            //TODO write your implementation code here:
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=Account", "sa", "1");
            PreparedStatement stm = null;
            stm = con.prepareStatement("SELECT * FROM Accounts WHERE Username LIKE ?");
            stm.setString(1, "%" + username + "%");
            ResultSet rs = stm.executeQuery();
            String s = "";
            while (rs.next()) {
                s += rs.getString("Username").trim() + "-" + rs.getString("Password") + "-";
            }
            return s;
        } catch (SQLException ex) {
            Logger.getLogger(LoginService.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(LoginService.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
}
